package task3.roi;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.TextField;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyEditor;

public class RectangleEditor
  extends Panel
  implements PropertyEditor
{
  private static final long serialVersionUID = 4729048621551063338L;
  private PropertyChangeSupport _support;
  private Rectangle _rectangle;
  private TextField _xField;
  private TextField _yField;
  private TextField _widthField;
  private TextField _heightField;
  
  public RectangleEditor()
  {
    this._rectangle = new Rectangle();
    this._support = new PropertyChangeSupport(this);
    
    Label xLabel = new Label("X:");
    Label yLabel = new Label("Y:");
    Label widthLabel = new Label("Width:");
    Label heightLabel = new Label("Height:");
    this._xField = new TextField(String.valueOf(this._rectangle.x));
    this._yField = new TextField(String.valueOf(this._rectangle.y));
    this._widthField = new TextField(String.valueOf(this._rectangle.width));
    this._heightField = new TextField(String.valueOf(this._rectangle.height));
    
    TextListener listener = new TextListener()
    {
      public void textValueChanged(TextEvent e)
      {
        if (e.getSource().equals(RectangleEditor.this._xField)) {
          RectangleEditor.this._rectangle.x = Integer.parseInt(RectangleEditor.this._xField.getText());
        } else if (e.getSource().equals(RectangleEditor.this._yField)) {
          RectangleEditor.this._rectangle.y = Integer.parseInt(RectangleEditor.this._yField.getText());
        } else if (e.getSource().equals(RectangleEditor.this._widthField)) {
          RectangleEditor.this._rectangle.width = Integer.parseInt(RectangleEditor.this._widthField.getText());
        } else if (e.getSource().equals(RectangleEditor.this._heightField)) {
          RectangleEditor.this._rectangle.height = Integer.parseInt(RectangleEditor.this._heightField.getText());
        }
        RectangleEditor.this._support.firePropertyChange("", null, null);
      }
    };
    this._xField.addTextListener(listener);
    this._yField.addTextListener(listener);
    this._widthField.addTextListener(listener);
    this._heightField.addTextListener(listener);
    
    setLayout(new GridLayout(4, 2, 5, 5));
    add(xLabel);
    add(this._xField);
    add(yLabel);
    add(this._yField);
    add(widthLabel);
    add(this._widthField);
    add(heightLabel);
    add(this._heightField);
  }
  
  public String getAsText()
  {
    StringBuilder builder = new StringBuilder();
    
    builder.append("X=")
      .append(this._rectangle.x)
      .append(" Y=")
      .append(this._rectangle.y)
      .append(" WIDTH=")
      .append(this._rectangle.width)
      .append(" HEIGHT=")
      .append(this._rectangle.height);
    
    return builder.toString();
  }
  
  public Component getCustomEditor()
  {
    return this;
  }
  
  public String[] getTags()
  {
    return null;
  }
  
  public Object getValue()
  {
    return this._rectangle;
  }
  
  public boolean isPaintable()
  {
    return true;
  }
  
  public void paintValue(Graphics gfx, Rectangle box)
  {
    gfx.drawString("Click to set ROI", 0, 22);
  }
  
  public void setValue(Object value)
  {
    this._rectangle = ((Rectangle)value);
    this._xField.setText(String.valueOf(this._rectangle.x));
    this._yField.setText(String.valueOf(this._rectangle.y));
    this._widthField.setText(String.valueOf(this._rectangle.width));
    this._heightField.setText(String.valueOf(this._rectangle.height));
  }
  
  public boolean supportsCustomEditor()
  {
    return true;
  }
  
  public String getJavaInitializationString()
  {
    return null;
  }
  
  public void setAsText(String text)
    throws IllegalArgumentException
  {}
  
  public void addPropertyChangeListener(PropertyChangeListener listener)
  {
    this._support.addPropertyChangeListener(listener);
  }
  
  public void removePropertyChangeListener(PropertyChangeListener listener)
  {
    this._support.removePropertyChangeListener(listener);
  }
}
